<template>
  <div>

      <img src="../assets/docx.jpg"  alt="word" class="image" @click="gotoedit">
    
    <div style="padding: 10px;">
      <span  style="display: inline-block; white-space: nowrap; width: 100%; overflow: hidden; text-overflow:ellipsis;">{{createItem.name}}</span>
      <div class="bottom clearfix">
        <!--        <time class="time">这里放什么时间？</time> -->
 
        
       
        <el-tooltip class="item" effect="dark" content="收藏" placement="bottom-start" v-if="!createItem.is_starred" style="position:relative;left:0px;top:15px;">
          <el-button type="warning" plain icon="el-icon-star-off" circle @click="handleLike" size="mini"></el-button>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="取消收藏" placement="bottom-start" v-else style="position:relative;left:0px;top:15px;">
          <el-button type="warning" icon="el-icon-star-off" circle @click="handleLike" size="mini"></el-button>
        </el-tooltip>

        <el-tooltip class="item" effect="dark" content="删除" placement="bottom-start"  style="position:relative;left:29px;top:-13px;">
          <el-button type="danger" icon="el-icon-delete" circle @click="handleGarbage" size="mini"></el-button>
        </el-tooltip>
      </div>
    </div>
  </div>
</template>

<script>
import {mapActions,mapState} from 'vuex'
import {reqLikeornotArticle,reqDeleteArticle} from '../api'
export default {
  name: "MyCreatedArticle",
  props:{
    createItem:Object,
  },
  data:function (){
    return{
      currentDate: new Date(),
    }
  },
  computed:{
    ...mapState(['userInfo','likeList','createList','onedit',])
  },
  mounted () {
    this.recordCreateList(this.userInfo.userid)
  },
  methods:{
    ...mapActions(['getLikeList','getGarbageList','getCreateList','getCurrentList','recordCreateList','recordOnEdit']),
    //点击收藏按钮
    handleLike(){
      this.createItem.is_starred=!this.createItem.is_starred
      if(this.createItem.is_starred){//收藏
        //调用收藏接口，修改数据库收藏列表
     
        
        const result=reqLikeornotArticle(this.createItem.id,true,this.userInfo.userid)
        //更新state中的收藏列表articleid,is_starred,userid
        console.log(result)

        setTimeout(() => {
          this.getLikeList()
          //更新回收站列表
        //  this.getGarbageList()
          //更新创建列表
          this.getCreateList()
          //更新浏览列表
        //  this.getCurrentList()
        },500)

        console.log(this.likeList.length)
      }
      else{//取消收藏
        //调用收藏接口，修改数据库收藏列表
        const result=reqLikeornotArticle(this.createItem.id,false,this.userInfo.userid)
        //更新state中的收藏列表
        setTimeout(() => {
          this.getLikeList();
          //更新回收站列表
         // this.getGarbageList()
          //更新创建列表
          this.getCreateList()
          //更新浏览列表
        //  this.getCurrentList()
        },500)
      
      }
    },
    //处理删除
    handleGarbage() {
      this.$alert('确定删除该文档吗？', '删除文档', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        //调用删除接口，删除文档，提交给数据库
        //从自己的创建文档列表删除
        console.log(this.createItem.id)
        console.log(this.userInfo.userid)
        const result=reqDeleteArticle(this.createItem.id,this.userInfo.userid)
        console.log(result)
        setTimeout(() => {
          //更新创建文档列表
          this.getCreateList()
          //如果收藏，从收藏列表中删除
          //更新state中的收藏列表
          this.getLikeList();
          //更新回收站列表
       //   this.getGarbageList()
          //更新浏览列表
         // this.getCurrentList()
        },500)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    gotoedit(){
      console.log('569')
      this.recordOnEdit(this.createItem.id)
      console.log(this.onedit)
      this.$router.replace('/edit')
    }
  }
}
</script>

<style>


.bottom {
  margin-top: 0px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 100%;
  display: block;
  margin-top: 0px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

</style>
